package net.sk.china.front.service.impl.order;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sk.china.common.model.order.StoreOrderInfo;
import net.sk.china.common.vo.OrderInfoDetailVo;
import net.sk.china.common.vo.StoreOrderInfoOldVo;
import net.sk.china.front.dao.order.StoreOrderInfoDao;
import net.sk.china.front.service.order.StoreOrderInfoService;
import net.sk.china.front.service.product.StoreProductReplyService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * StoreOrderInfoServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2018-2024 聊来All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * +----------------------------------------------------------------------
 * | @Author: 聊来 <18970881148@qq.com>
 * +----------------------------------------------------------------------
 * | DateTime: 2024/6/12 21:28
 * +----------------------------------------------------------------------
 */
@Service
public class StoreOrderInfoServiceImpl extends ServiceImpl<StoreOrderInfoDao, StoreOrderInfo> implements StoreOrderInfoService {

    @Resource
    private StoreOrderInfoDao dao;

    private final StoreProductReplyService storeProductReplyService;

    public StoreOrderInfoServiceImpl(@Lazy StoreProductReplyService storeProductReplyService) {
        this.storeProductReplyService = storeProductReplyService;
    }


    /**
     * 获取订单详情vo列表
     * @param orderId 订单id
     * @return {@code List<StoreOrderInfoOldVo>}
     */
    @Override
    public List<StoreOrderInfoOldVo> getVoListByOrderId(Integer orderId) {
        LambdaQueryWrapper<StoreOrderInfo> query = new LambdaQueryWrapper<>();
        query.eq(StoreOrderInfo::getOrderId, orderId);
        List<StoreOrderInfo> systemStoreStaffList = dao.selectList(query);
        if(systemStoreStaffList.size() < 1){
            return null;
        }

        List<StoreOrderInfoOldVo> storeOrderInfoVoList = new ArrayList<>();
        for (StoreOrderInfo storeOrderInfo : systemStoreStaffList) {
            //解析商品详情JSON
            StoreOrderInfoOldVo storeOrderInfoVo = new StoreOrderInfoOldVo();
            BeanUtils.copyProperties(storeOrderInfo, storeOrderInfoVo, "info");
            storeOrderInfoVo.setInfo(JSON.parseObject(storeOrderInfo.getInfo(), OrderInfoDetailVo.class));
            storeOrderInfoVoList.add(storeOrderInfoVo);
        }
        return storeOrderInfoVoList;
    }

    /**
     * 批量添加订单详情
     * @param storeOrderInfos 订单详情集合
     * @return {@code boolean}
     */
    @Override
    public boolean saveOrderInfos(List<StoreOrderInfo> storeOrderInfos) {
        return saveBatch(storeOrderInfos);
    }

    /**
     * 获取订单详情-订单编号
     * @param orderNo 订单编号
     * @return {@code List<StoreOrderInfo>}
     */
    @Override
    public List<StoreOrderInfo> getListByOrderNo(String orderNo) {
        LambdaQueryWrapper<StoreOrderInfo> query = new LambdaQueryWrapper<>();
        query.eq(StoreOrderInfo::getOrderNo, orderNo);

        return dao.selectList(query);
    }

    /**
     * 根据id集合查询数据，返回 map
     * @param orderId 订单ID
     * @return {@code List<StoreOrderInfoOldVo>}
     */
    @Override
    public List<StoreOrderInfoOldVo> getOrderListByOrderId(Integer orderId) {
        LambdaQueryWrapper<StoreOrderInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StoreOrderInfo::getOrderId, orderId);
        List<StoreOrderInfo> systemStoreStaffList = dao.selectList(lambdaQueryWrapper);
        if(systemStoreStaffList.size() < 1){
            return null;
        }

        List<StoreOrderInfoOldVo> storeOrderInfoVoList = new ArrayList<>();
        for (StoreOrderInfo storeOrderInfo : systemStoreStaffList) {
            //解析商品详情JSON
            StoreOrderInfoOldVo storeOrderInfoVo = new StoreOrderInfoOldVo();
            BeanUtils.copyProperties(storeOrderInfo, storeOrderInfoVo, "info");
            storeOrderInfoVo.setInfo(JSON.parseObject(storeOrderInfo.getInfo(), OrderInfoDetailVo.class));
            storeOrderInfoVo.getInfo().setIsReply(
                    storeProductReplyService.isReply(storeOrderInfoVo.getUnique(), storeOrderInfoVo.getOrderId()) ? 1 : 0
            );
            storeOrderInfoVoList.add(storeOrderInfoVo);
        }
        return storeOrderInfoVoList;
    }

    /**
     * 通过订单编号和规格号查询
     * @param unique 规格ID
     * @param orderId 订单编号
     * @return {@code StoreOrderInfo}
     */
    @Override
    public StoreOrderInfo getByUniAndOrderId(String unique, Integer orderId) {
        LambdaQueryWrapper<StoreOrderInfo> query = new LambdaQueryWrapper<>();
        query.eq(StoreOrderInfo::getOrderId, orderId);
        query.eq(StoreOrderInfo::getUnique, unique);
        query.last("limit 1");

        return dao.selectOne(query);
    }
}
